const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('mainapp', {
  node: () => process.versions.node,
  chrome: () => process.versions.chrome,
  electron: () => process.versions.electron,
  ping: () => ipcRenderer.invoke('ping'),
  onUpdateCounter: (callback) => ipcRenderer.on('update-counter', (_event, value) => callback(value)),
  counterValue: (value) => ipcRenderer.send('counter-value', value),
    // 渲染进程调用：window.electronAPI.sendToMain('event', data)
  sendToMain: (channel, data) => ipcRenderer.send(channel, data),
  // 渲染进程监听主进程消息：window.electronAPI.onMainMessage('event', callback)
  onMainMessage: (channel, callback) => ipcRenderer.on(channel, (event, ...args) => callback(...args))
})